﻿import cgi
import os 
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp                                        
from google.appengine.ext.webapp.util import run_wsgi_app                      
import logging                                                                 
 
from StringIO import StringIO                                                  
import traceback
import simplejson
import xmlrpclib
from xmlrpcserver import XmlRpcServer
from google.appengine.api import users
from entities import *
class Application:
    def __init__(self):
        pass                    
        
    #------------------------------------------
    #- LinhVuc
    #------------------------------------------
    
    def GetTopApp(self,meta,domain,isFree):
        result = App.GetTopApp(domain,isFree,1)
        xmlrpccall = xmlrpclib.dumps(result, 'GetTopApp')
        return xmlrpccall
        
    def GetbyCategory(self,meta,domain,categoryName):
        if categoryName == "Hot":
            result = App.GetHotApp(domain,1)
        else:
            result = App.GetbyCategory(domain,categoryName)
            
        xmlrpccall = xmlrpclib.dumps(result, 'GetbyCategory')
        return xmlrpccall

    def GetHotApp(self,meta,domain):
        result = App.GetHotApp(domain,1)
        xmlrpccall = xmlrpclib.dumps(result,'GetHotApp')
        return xmlrpccall
    
    def Download(self,meta,appID):
        result = App.Download(appID)
        xmlrpccall = xmlrpclib.dumps(result,'Download')
        return xmlrpccall
        
    def GetComment(self,meta,appID,isFull):
        result = AppComment.GetbyApp(appID,isFull)
        xmlrpccall = xmlrpclib.dumps(result, 'GetComment')
        return xmlrpccall
    
    def AddComment(self,meta,commentContent,votes,appID,customerName):
        result = AppComment.Add(commentContent,votes,appID,customerName)
        return result
        
    def getGenre(self,meta):
        test= [{'job': 'student', 'name': 'Long'}, {'job': 'student', 'name': 'An dam'}]
        tuple_test = tuple(test)
        xmlrpccall = xmlrpclib.dumps(tuple_test, 'GetAllGreeting')
        return xmlrpccall
        #return test
#-----------------------------------------
#- webservice XMLRPC
#-----------------------------------------
class XMLRpcHandler(webapp.RequestHandler):                                    
    rpcserver = None
                                
    def __init__(self):         
        self.rpcserver = XmlRpcServer()                                        
        app = Application()                                                    
        self.rpcserver.register_class('app',app)                               
                                                                               
    def post(self):
        request = StringIO(self.request.body)
        request.seek(0)                                                        
        response = StringIO()                                                  
        try:
            self.rpcserver.execute(request, response, None)                    
        except Exception, e:                                                   
            logging.error('Error executing: '+str(e))                          
            for line in traceback.format_exc().split('\n'):                    
                logging.error(line)
        finally:
            response.seek(0)  
                                                                               
        rstr = response.read()                                                 
        self.response.headers['Content-type'] = 'text/xml'                     
        self.response.headers['Content-length'] = "%d"%len(rstr)               
        self.response.out.write(rstr)
